package com.esun.rabbit2.viewer.mainact.lotteryinfoact;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.model.CurrentLotteryNumInfo;
import com.esun.model.UserInfo;
import com.esun.rabbit2.communicaters.CurrentLotteryNumCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.DaletouAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.ShuangseqiuAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.ShiyixuanwuActGroup;
import com.esun.ui.R;
import com.esun.util.log.EDebug;
import com.esun.util.webviewchromeclient.MyWebViewChromeClient;

/**
 * @ClassName: LatestdetailsAct
 * @Description: 最新开奖信息
 * @author: Kael gaojian@500wan.com
 * @date: 2012-7-24 上午11:09:00
 * 
 */
public class LatestdetailsAct extends Activity implements OnClickListener {
	private WebView latestDetailsWebView;
	private String type;
	private String url;
	private CurrentLotteryNumCommunicater currentLotteryNumCommunicater;
	private String lotid;
	public static Handler handlerLatestsetailsAct;
	private ImageView ivLotIcon;
	private TextView tvLotName;
	private TextView tvLotExpect;
	private TextView tvLotDate;
	private LinearLayout llLotNum;
	private RelativeLayout relativeProgress;
	private CurrentLotteryNumInfo lotteryNumInfo;
	private ProgressBar pbProgress;
	private TextView tvGetting;
	private Button btnBuy;
	private Button btnLotteryRecord;
	public static final String SHUANGSEQIUAGPNAME = "ShuangseqiuAgp";
	public static final String SHIYIXUANWUAGPNAME = "ShiyixuanwuActGroup";
	public static final String DALETOUAGPNAME = "DaletouAgp";
	public static final String LOTTERYINFOLISTAGPNAME = "LotteryInfoListAgp";

	/** 获取上期中奖号码成功 */
	private static final int GET_LOTTERY_NUM_SUCCEED = 10007;
	/** 获取上期中奖号码失败 */
	private static final int GET_LOTTERY_NUM_FAILED = 10008;
	public static final int JUMPTOLATESTDETAIL = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lotteryinfolist_latestdetails_webview_act);
		init();
		initHandler();
		threadGetCurrentLotteryNum(lotid);
		latestDetailsWebView.loadUrl(url);
	}

	private void init() {
		type = getIntent().getExtras().getString(Key.KEY_TYPE);
		lotid = getIntent().getExtras().getString(Key.KEY_LOTID);

		ivLotIcon = (ImageView) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_image);
		tvLotName = (TextView) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_lotid);
		tvLotExpect = (TextView) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_expect);
		tvLotDate = (TextView) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_date);
		llLotNum = (LinearLayout) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_ballnum);
		pbProgress = (ProgressBar) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_progressbar);
		tvGetting = (TextView) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_huoquzhong);
		btnBuy = (Button) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_buy);
		btnBuy.setOnClickListener(this);
		btnLotteryRecord = (Button) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_lotteryrecord);
		btnLotteryRecord.setOnClickListener(this);
		if (lotid.equals(Config.FUCAI3D)) {
			btnBuy.setVisibility(View.GONE);
			btnLotteryRecord.setVisibility(View.GONE);
		}
		latestDetailsWebView = (WebView) findViewById(R.id.lotteryinfolist_latestdetails_webview_act_webview);
		url = Config.ACTIVITY_LOTTERYINFO_URL + "?type=" + type;
		latestDetailsWebView.setBackgroundColor(0);
		WebSettings webSettings = latestDetailsWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setSaveFormData(false);
		webSettings.setSavePassword(false);
		webSettings.setSupportZoom(false);
		relativeProgress = (RelativeLayout) findViewById(R.id.lotteryinfolist_latestdetails_act_relative_progress);
		latestDetailsWebView.setWebChromeClient(new MyWebViewChromeClient(relativeProgress));
		if (lotid.equals(Config.SHUANGSEQIU)) {
			ivLotIcon.setImageResource(R.drawable.img_default_boughtcenter_doubleball_53x53);
			tvLotName.setText("双色球");
		} else if (lotid.equals(Config.DALETOU)) {
			ivLotIcon.setImageResource(R.drawable.img_default_boughtcenter_lotto_45x45);
			tvLotName.setText("大乐透");
		} else if (lotid.equals(Config.SHIYIXUANWU)) {
			ivLotIcon.setImageResource(R.drawable.img_default_boughtcenter_11pick5_45x45);
			tvLotName.setText("十一选五");
		} else if (lotid.equals(Config.FUCAI3D)) {
			ivLotIcon.setImageResource(R.drawable.img_default_boughtcenter_3d_45x45);
			tvLotName.setText("福彩3D");
		}
	}

	private void initHandler() {
		handlerLatestsetailsAct = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case GET_LOTTERY_NUM_SUCCEED:
					getSucceed();
					break;
				case GET_LOTTERY_NUM_FAILED:
					pbProgress.setVisibility(View.GONE);
					tvGetting.setText(currentLotteryNumCommunicater.getServerMsg());
					break;
				case JUMPTOLATESTDETAIL:
					doRefresh(msg);
					break;
				default:
					break;
				}
			}

		};
	}

	/**
	 * @Title: threadGetCurrentLotteryNum
	 * @Description: 获取当前开奖号码
	 * @param: @param lotid 设定文件
	 * @return: void 返回类型
	 * 
	 */
	private void threadGetCurrentLotteryNum(final String lotid) {
		new Thread() {

			@Override
			public void run() {
				super.run();
				Message msg;
				try {
					if (currentLotteryNumCommunicater == null) {
						currentLotteryNumCommunicater = new CurrentLotteryNumCommunicater();
					}
					boolean isGetLotteryNumSucceed = currentLotteryNumCommunicater.isGetCurrentLotteryNumSucceed(lotid);
					if (isGetLotteryNumSucceed) {
						msg = handlerLatestsetailsAct.obtainMessage(GET_LOTTERY_NUM_SUCCEED, null);
					} else {
						msg = handlerLatestsetailsAct.obtainMessage(GET_LOTTERY_NUM_FAILED, null);
					}
					handlerLatestsetailsAct.sendMessage(msg);
				} catch (Exception e) {
					msg = handlerLatestsetailsAct.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerLatestsetailsAct.sendMessage(msg);
				}

			}

		}.start();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lotteryinfolist_latestdetails_webview_act_buy:

			jumpToBuyLottery();

			break;
		case R.id.lotteryinfolist_latestdetails_webview_act_lotteryrecord:

			if (UserInfo.getUserInfo().getUserSession().length() > 0) {
				jumpToLotteryRecord();
			} else {
				startActivityForResult(new Intent(LatestdetailsAct.this, LoginAct.class), 2);
			}

			break;
		default:
			break;
		}

	}

	private void jumpToBuyLottery() {
		Activity parent = this.getParent();
		String parentName = parent.getClass().getName();
		EDebug.o(this, "parent name = " + parentName + " name = " + LotteryInfoListAgp.class.getName());
		if (parentName.equals(LotteryInfoListAgp.class.getName())) {
			EDebug.o(this, "lottery id = " + lotid);
			if (lotid.equals(Config.SHUANGSEQIU)) {
				startActivity(new Intent(this, ShuangseqiuAgp.class).putExtra(Config.IS_REBUY, false));
			} else if (lotid.equals(Config.DALETOU)) {
				startActivity(new Intent(this, DaletouAgp.class).putExtra(Config.IS_REBUY, false));
			} else if (lotid.equals(Config.SHIYIXUANWU)) {
				startActivity(new Intent(this, ShiyixuanwuActGroup.class).putExtra(Config.IS_REBUY, false));
			} else {
				EDebug.o(this, "暂不支持");
			}
		} else if (parentName.equals(ShuangseqiuAgp.class.getName())) {
			((ShuangseqiuAgp) parent).addViewActivity(ShuangseqiuAgp.CHOOSE);
		} else if (parentName.equals(DaletouAgp.class.getName())) {
			((DaletouAgp) parent).addViewActivity(DaletouAgp.CHOOSE);
		} else if (parentName.equals(ShiyixuanwuActGroup.class.getName())) {
			((ShiyixuanwuActGroup) parent).addViewActivity(ShiyixuanwuActGroup.CHOOSE);
		} else {
			EDebug.o(this, "错误");
		}
	}

	private void jumpToLotteryRecord() {

		Activity parent2 = this.getParent();
		String parentName2 = parent2.getClass().getName();
		EDebug.o(this, "parent name = " + parentName2 + " name = " + LotteryInfoListAgp.class.getName());
		if (parentName2.equals(LotteryInfoListAgp.class.getName())) {
			EDebug.o(this, "lottery id = " + lotid);
			if (lotid.equals(Config.SHUANGSEQIU)) {
				startActivity(new Intent(this, ShuangseqiuAgp.class).putExtra(Config.CURRENT_TAB_NUM, ShuangseqiuAgp.LOTTERYRECORD).putExtra(Config.IS_REBUY, false));
			} else if (lotid.equals(Config.DALETOU)) {
				startActivity(new Intent(this, DaletouAgp.class).putExtra(Config.CURRENT_TAB_NUM, DaletouAgp.LOTTERYRECORD).putExtra(Config.IS_REBUY, false));
			} else if (lotid.equals(Config.SHIYIXUANWU)) {
				startActivity(new Intent(this, ShiyixuanwuActGroup.class).putExtra(Config.CURRENT_TAB_NUM, ShiyixuanwuActGroup.LOTTERYRECORD).putExtra(Config.IS_REBUY, false));
			} else {
				EDebug.o(this, "暂不支持");
			}
		} else if (parentName2.equals(ShuangseqiuAgp.class.getName())) {
			((ShuangseqiuAgp) parent2).addViewActivity(ShuangseqiuAgp.LOTTERYRECORD);
		} else if (parentName2.equals(DaletouAgp.class.getName())) {
			((DaletouAgp) parent2).addViewActivity(DaletouAgp.LOTTERYRECORD);
		} else if (parentName2.equals(ShiyixuanwuActGroup.class.getName())) {
			((ShiyixuanwuActGroup) parent2).addViewActivity(ShiyixuanwuActGroup.LOTTERYRECORD);
		} else {
			EDebug.o(this, "错误");
		}

	}

	private void getSucceed() {
		pbProgress.setVisibility(View.GONE);
		tvGetting.setVisibility(View.GONE);
		tvLotExpect.setVisibility(View.VISIBLE);
		tvLotDate.setVisibility(View.VISIBLE);
		llLotNum.setVisibility(View.VISIBLE);
		lotteryNumInfo = currentLotteryNumCommunicater.getCurrentLotteryNum().get(0);
		String expect = lotteryNumInfo.getLastexpect();
		tvLotExpect.setText("第" + expect + "期");

		String time = lotteryNumInfo.getLastopentime();
		tvLotDate.setText(time);

		String lastopencode = lotteryNumInfo.getLastopencode();

		if (lastopencode.contains("|")) {
			String[] opencode = lastopencode.split("\\|");
			String[] redcode = opencode[0].toString().split(Config.COMMA);

			for (int i = 0; i < redcode.length; i++) {
				TextView tvRedCode = new TextView(LatestdetailsAct.this);
				LinearLayout llRedCode = new LinearLayout(LatestdetailsAct.this);
				if (i != 0) {
					llRedCode.setPadding(8, 0, 0, 0);
					if (i == redcode.length - 1) {
						llRedCode.setPadding(8, 0, 8, 0);
					}
				}
				tvRedCode.setText(redcode[i]);
				tvRedCode.setTextColor(getResources().getColor(R.color.white));
				tvRedCode.setBackgroundResource(R.drawable.img_ball_red_36x39);
				tvRedCode.setGravity(Gravity.CENTER);
				tvRedCode.setTextSize(13);
				llRedCode.addView(tvRedCode);
				llLotNum.addView(llRedCode);
			}
			if (opencode[1].contains(Config.COMMA)) {
				String[] bluecode = opencode[1].toString().split(Config.COMMA);
				for (int i = 0; i < bluecode.length; i++) {
					TextView tvBlueCode = new TextView(LatestdetailsAct.this);
					LinearLayout llBlueCode = new LinearLayout(LatestdetailsAct.this);
					llBlueCode.setPadding(8, 0, 0, 0);
					tvBlueCode.setText(bluecode[i]);
					tvBlueCode.setTextColor(getResources().getColor(R.color.white));
					tvBlueCode.setBackgroundResource(R.drawable.img_ball_blue_36x39);
					tvBlueCode.setGravity(Gravity.CENTER);
					tvBlueCode.setTextSize(13);
					llBlueCode.addView(tvBlueCode);
					llLotNum.addView(llBlueCode);
				}
			} else {
				TextView tvBlueCode = new TextView(LatestdetailsAct.this);
				tvBlueCode.setText(opencode[1]);
				tvBlueCode.setTextColor(getResources().getColor(R.color.white));
				tvBlueCode.setBackgroundResource(R.drawable.img_ball_blue_36x39);
				tvBlueCode.setGravity(Gravity.CENTER);
				tvBlueCode.setTextSize(13);
				llLotNum.addView(tvBlueCode);
			}
		} else {
			String[] code = lastopencode.split(Config.COMMA);
			for (int i = 0; i < code.length; i++) {

				TextView Code = new TextView(LatestdetailsAct.this);
				LinearLayout llCode = new LinearLayout(LatestdetailsAct.this);
				if (i != 0) {
					llCode.setPadding(8, 0, 0, 0);
					if (i == code.length - 1) {
						llCode.setPadding(8, 0, 8, 0);
					}
				}
				Code.setText(code[i]);
				Code.setTextColor(getResources().getColor(R.color.white));
				Code.setBackgroundResource(R.drawable.img_ball_red_36x39);
				Code.setGravity(Gravity.CENTER);
				Code.setTextSize(13);
				llCode.addView(Code);
				llLotNum.addView(llCode);
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {

		case 2:

			switch (resultCode) {
			case RESULT_OK:

				jumpToLotteryRecord();

				break;

			case RESULT_CANCELED:

				break;

			default:
				break;
			}

			break;

		default:
			break;

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/** 
	* @Title: setLotteryInfo 
	* @Description: 重新设置本期开奖信息显示
	* @param: @param red
	* @param: @param blue
	* @param: @param expect
	* @param: @param opentime    设定文件 
	* @return: void    返回类型 
	* @throws 
	*/
	@SuppressWarnings("null")
	private void setLotteryInfo(String red, String blue, String expect, String opentime) {
		pbProgress.setVisibility(View.GONE);
		tvGetting.setVisibility(View.GONE);
		tvLotExpect.setVisibility(View.VISIBLE);
		tvLotDate.setVisibility(View.VISIBLE);
		llLotNum.setVisibility(View.VISIBLE);
		tvLotExpect.setText("第" + expect + "期");
		tvLotDate.setText(opentime);
		llLotNum.removeAllViews();
		String[] ballred = red.split(Config.ONESPACE);
		for (int i = 0; i < ballred.length; i++) {
			TextView tvRedCode = new TextView(LatestdetailsAct.this);
			LinearLayout llRedCode = new LinearLayout(LatestdetailsAct.this);
			if (i != 0) {
				llRedCode.setPadding(8, 0, 0, 0);
				if (i == ballred.length - 1) {
					llRedCode.setPadding(8, 0, 8, 0);
				}
			}
			tvRedCode.setText(ballred[i]);
			tvRedCode.setTextColor(getResources().getColor(R.color.white));
			tvRedCode.setBackgroundResource(R.drawable.img_ball_red_36x39);
			tvRedCode.setGravity(Gravity.CENTER);
			tvRedCode.setTextSize(13);
			llRedCode.addView(tvRedCode);
			llLotNum.addView(llRedCode);

		}
		String[] ballblue;
		if (blue != null || !blue.equals(Config.NULLMARK)) {
			ballblue = blue.split(Config.ONESPACE);
			for (int j = 0; j < ballblue.length; j++) {
				if (ballblue[j] != null && !ballblue[j].equals(Config.NULLMARK)) {
				TextView tvBlueCode = new TextView(LatestdetailsAct.this);
				LinearLayout llBlueCode = new LinearLayout(LatestdetailsAct.this);
				llBlueCode.setPadding(8, 0, 0, 0);
				tvBlueCode.setText(ballblue[j]);
				tvBlueCode.setTextColor(getResources().getColor(R.color.white));
				tvBlueCode.setBackgroundResource(R.drawable.img_ball_blue_36x39);
				tvBlueCode.setGravity(Gravity.CENTER);
				tvBlueCode.setTextSize(13);
				llBlueCode.addView(tvBlueCode);
				llLotNum.addView(llBlueCode);
				}
				
			}
		}
	}

	/** 
	* @Title: doRefresh 
	* @Description: 刷新本期开奖信息
	* @param: @param msg    设定文件 
	* @return: void    返回类型 
	* @throws 
	*/
	private void doRefresh(Message msg) {
		Bundle jumpBundle = msg.getData();
		String expect = jumpBundle.getString(Key.KEY_EXPECT);
		String redball = jumpBundle.getString(Key.KEY_LOTTERYNUMRED);
		String blueball = jumpBundle.getString(Key.KEY_LOTTERYNUMBLUE);
		String opentime = jumpBundle.getString(Key.KEY_OPENTIME);
		setLotteryInfo(redball, blueball, expect, opentime);
		url = Config.ACTIVITY_LOTTERYINFO_URL + "?type=" + type + "&expect=" + expect;
		latestDetailsWebView.loadUrl(url);
	}

}
